import {getMacAddress} from "@/utils/auth";

export class BluetoothPrint {
    #blt;
    #that;
    #print;
    #connectionStatus = false;
    #printFlag = false;
    #printCallbackFn;
    #printParam;

    constructor(that, callbackFn) {
        this.#that = that;
        this.#blt = window.api.require("cxgBluetoothBase");
        this.#print = window.api.require("xGprinterPlus");
        this.#printCallbackFn = callbackFn;
        this.isEnabledBluetooth();
    }

    //判断蓝牙是否已打开
    isEnabledBluetooth() {
        const that = this;
        that.#blt.isEnabledBluetooth({}, (ret, err) => {
            if (!err) {
                if (ret.state) {
                    // 已打开
                    that.connectBleByMacAddress();
                } else {
                    that.openBluetooth();
                }
            } else {
                that.#that.$toast.fail("code: " + err.code + " msg: " + err.msg);
            }
        });
    }

    // 打开蓝牙
    openBluetooth() {
        const that = this;
        that.#blt.openBluetooth({}, function (ret, err) {
            if (!err) {
                if (ret.state) {
                    // 已打开
                    that.connectBleByMacAddress();
                }
            } else {
                that.listenBluetoothStatus();
            }
        });
    }

    // 监听蓝牙连接状态
    listenBluetoothStatus() {
        const that = this;
        that.#blt.listenBluetoothStatus({}, function (ret, err) {
            if (!err) {
                if (ret.status === "STATE_ON") {
                    that.connectBleByMacAddress();
                }
            } else {
                that.#that.$toast.fail("code: " + err.code + " msg: " + err.msg);
            }
        });
    }

    // 连接蓝牙
    connectBleByMacAddress() {
        const that = this;
        if (that.#printFlag) {
            that.#that.$toast({
                message: "初始化打印机",
                type: "loading",
                duration: 20000,
            });
        }
        that.#print.connectBleByMacAddress({
            macAddress: getMacAddress() ? getMacAddress().address : "",
        }, (ret, err) => {
            if (ret.status) {
                if (ret.message === "已连接") {
                    that.#connectionStatus = true;
                    if (that.#printFlag) {
                        that.#that.$toast({
                            type: "success",
                            message: "连接成功,开始打印",
                            duration: 1500,
                            onClose: () => {
                                that.printLabel();
                            },
                        });
                    }
                } else if (ret.message === "连接失败") {
                    that.#connectionStatus = false;
                    that.#that.$toast({
                        type: "fail",
                        message: "初始化失败",
                        duration: 1500,
                    });
                } else {
                    that.#connectionStatus = false;
                }
            } else {
                that.#that.$toast({
                    type: "fail",
                    message: err.msg,
                    duration: 1500,
                });
            }
        });
    }

    printLabel() {
        const that = this;
        that.#print.printLabel({
            rows: that.#printParam
        }, (ret, err) => {
            if(ret.status) {
                that.#printCallbackFn();
            } else {
                that.#that.$toast({
                    type: "fail",
                    message: err.code + err.msg,
                    duration: 1500,
                    /*onClose: () => {
                        that.#connectBleByMacAddress();
                    },*/
                });
            }
        });
    }

    // 查询打印机连接状态
    print(param) {
        const that = this;
        that.#printParam = param;
        that.#that.$toast({
            message: "正在打印",
            type: "loading",
            duration: 20000,
        });
        that.#printFlag = true;
        let ret = that.#print.getConnectState();
        if (ret.status) {
            let result = false;
            ret.state.forEach(bool => {
                if (bool) {
                    result = bool;
                }
            });
            if (result) {
                // 已连接
                that.#that.$toast.clear();
                if (that.#printFlag) {
                    that.printLabel();
                }
            } else {
                // 未连接
                that.#that.$toast({
                    type: "fail",
                    message: "未连接, 请等待",
                    duration: 1500,
                    onClose: () => {
                        that.isEnabledBluetooth();
                    },
                });
            }
        } else {
            // 未连接
            that.#that.$toast({
                type: "fail",
                message: "未连接, 请等待",
                duration: 1500,
                onClose: () => {
                    that.connectBleByMacAddress();
                },
            });
        }
    }
}
